﻿package com.tudou.ad
{
    import com.tudou.ad.consts.*;
    import com.tudou.ad.data.*;
    import com.tudou.ad.interfaces.*;
    import com.tudou.ad.track.*;
    import com.tudou.config.*;
    import com.tudou.events.*;
    import com.tudou.model.*;
    import com.tudou.track.*;
    import com.tudou.utils.*;
    import flash.events.*;
    import flash.net.*;

    public class AdLoader extends EventDispatcher
    {
        protected var mainInfo:IMainInfo;
        protected var loader:URLLoader;
        protected var _adCacheList:XMLList;
        protected var adData:AdData;
        protected var url:String;
        protected var miniLiveXml:XML;
        protected var _rurlId:String;

        public function AdLoader()
        {
            adData = AdData.getInst();
            return;
        }// end function

        public function removeAdItemByTid(param1:int) : void
        {
            adData.removeAdItemByTid(param1);
            return;
        }// end function

        protected function parseAllTid(param1:XML) : void
        {
            var _loc_2:XMLList = null;
            var _loc_3:XML = null;
            var _loc_5:int = 0;
            _loc_2 = param1.ads;
            var _loc_4:Array = [];
            if (_loc_2.length() > 0)
            {
                _loc_5 = 0;
                while (_loc_5 < _loc_2.length())
                {
                    
                    _loc_3 = _loc_2[_loc_5];
                    _loc_4.push(_loc_3.@aid);
                    _loc_5++;
                }
            }
            adData.adIds = _loc_4;
            return;
        }// end function

        public function getCurrAdItemByType(param1:int) : IAdItemInfo
        {
            return adData.getCurrAdItemByType(param1);
        }// end function

        protected function getBackgroundAdInfo(param1:XML) : void
        {
            var listXML:XMLList;
            var itemXML:XML;
            var info:* = param1;
            var _loc_4:int = 0;
            var _loc_5:* = info.ads;
            var _loc_3:* = new XMLList("");
            for each (_loc_6 in _loc_5)
            {
                
                var _loc_7:* = _loc_5[_loc_4];
                with (_loc_5[_loc_4])
                {
                    if (@type == AdType.BACKGROUND)
                    {
                        _loc_3[_loc_4] = _loc_6;
                    }
                }
            }
            listXML = _loc_3;
            itemXML = listXML.length() > 0 ? (listXML[0]) : (null);
            if (itemXML != null)
            {
                adData.setAdItemByType(new AdItemInfo(itemXML), AdType.BACKGROUND);
            }
            return;
        }// end function

        protected function loadAdInfo() : void
        {
            closeLoader();
            loader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, completeHlr);
            loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHlr);
            loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHlr);
            loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHlr);
            var _loc_1:* = new URLRequest();
            var _loc_2:* = new URLVariables();
            _loc_2.juid = mainInfo.juid;
            _loc_2.channelId = mainInfo.channelId;
            _loc_2.itemId = mainInfo.iid;
            _loc_2.adPlay = TudouAdPlay.getValue();
            _loc_2.adSourceId = mainInfo.adSourceId;
            _loc_2.userId = mainInfo.userId;
            _loc_2.refer = mainInfo.href;
            _loc_2.ws = mainInfo.viewState;
            if (mainInfo.isListPlayer)
            {
                _loc_2.playlistId = mainInfo.lid;
            }
            if (mainInfo.actionID != "0")
            {
                _loc_2.throwActivityId = mainInfo.actionID;
            }
            _loc_2.tpa = mainInfo.tpa;
            _loc_2.rand = RandString.noCache;
            _loc_2.rurl = mainInfo.refferURL;
            _loc_2.rid = mainInfo.rurlId;
            _loc_2.rinfo = mainInfo.rinfo;
            _loc_1.url = Config.ADCONTROL_URL;
            _loc_1.method = URLRequestMethod.POST;
            _loc_1.data = _loc_2;
            url = _loc_1.url + "?" + _loc_2.toString();
            loader.load(_loc_1);
            return;
        }// end function

        public function load(param1:IMainInfo) : void
        {
            this.mainInfo = param1;
            adData.reset();
            loadAdInfo();
            return;
        }// end function

        protected function getOnline(param1:XML) : void
        {
            var _loc_2:* = param1.live;
            miniLiveXml = _loc_2[0];
            return;
        }// end function

        public function removeAdItemByType(param1:int) : void
        {
            adData.removeAdItemByType(param1);
            return;
        }// end function

        private function ioErrorHlr(event:IOErrorEvent) : void
        {
            var _loc_2:String = "ioError";
            ErrorReportTracker.send(mainInfo.iid, ErrorReportTracker.ADCONTROL_IOERROR, _loc_2);
            ErrorTracker.send(mainInfo.sourceId, ErrorTracker.ADCONTROL_IOERROR, 0, url);
            dispatchEvent(new AdLoaderEvent(AdLoaderEvent.IO_ERROR, false, false, _loc_2));
            return;
        }// end function

        protected function getPlacardAndVideoInfo(param1:XML) : void
        {
            var placardXML:XML;
            var listXML:XMLList;
            var itemXML:XML;
            var itemInfo:IAdItemInfo;
            var frontArr:Array;
            var i:int;
            var info:* = param1;
            var pos:int;
            var _loc_4:int = 0;
            var _loc_5:* = info.ads;
            var _loc_3:* = new XMLList("");
            for each (_loc_6 in _loc_5)
            {
                
                var _loc_7:* = _loc_5[_loc_4];
                with (_loc_5[_loc_4])
                {
                    if (@type == AdType.PLACARD)
                    {
                        _loc_3[_loc_4] = _loc_6;
                    }
                }
            }
            listXML = _loc_3;
            placardXML = listXML.length() > 0 ? (listXML[0]) : (null);
            if (placardXML != null)
            {
                itemInfo = new AdItemInfo(placardXML);
                pos = (pos + 1);
                itemInfo.orderPos = pos;
                adData.setAdItemByType(itemInfo, AdType.PLACARD);
            }
            var _loc_4:int = 0;
            var _loc_5:* = info.ads;
            var _loc_3:* = new XMLList("");
            for each (_loc_6 in _loc_5)
            {
                
                var _loc_7:* = _loc_5[_loc_4];
                with (_loc_5[_loc_4])
                {
                    if (@type == AdType.FRONT)
                    {
                        _loc_3[_loc_4] = _loc_6;
                    }
                }
            }
            listXML = _loc_3;
            if (listXML.length() > 0)
            {
                frontArr;
                i;
                while (i < listXML.length())
                {
                    
                    itemXML = listXML[i];
                    itemInfo = new AdItemInfo(itemXML);
                    pos = (pos + 1);
                    itemInfo.orderPos = pos;
                    frontArr.push(itemInfo);
                    i = (i + 1);
                }
                adData.setAdItemByType(frontArr, AdType.FRONT);
            }
            return;
        }// end function

        public function get liveXml() : XML
        {
            return miniLiveXml;
        }// end function

        public function get throwInfo() : IAdThrowInfo
        {
            return adData.adThrowInfo;
        }// end function

        protected function getLoadingAdInfo(param1:XML) : void
        {
            var listXML:XMLList;
            var itemXML:XML;
            var itemInfo:IAdItemInfo;
            var loadingArr:Array;
            var i:int;
            var info:* = param1;
            var _loc_4:int = 0;
            var _loc_5:* = info.ads;
            var _loc_3:* = new XMLList("");
            for each (_loc_6 in _loc_5)
            {
                
                var _loc_7:* = _loc_5[_loc_4];
                with (_loc_5[_loc_4])
                {
                    if (@type == AdType.LOADING)
                    {
                        _loc_3[_loc_4] = _loc_6;
                    }
                }
            }
            listXML = _loc_3;
            if (listXML.length() > 0)
            {
                loadingArr;
                i;
                while (i < listXML.length())
                {
                    
                    itemXML = listXML[i];
                    itemInfo = new AdItemInfo(itemXML);
                    loadingArr.push(itemInfo);
                    i = (i + 1);
                }
                adData.setAdItemByType(loadingArr, AdType.LOADING);
            }
            return;
        }// end function

        protected function getPauseAdInfo(param1:XML) : void
        {
            var listXML:XMLList;
            var itemXML:XML;
            var itemInfo:IAdItemInfo;
            var i:int;
            var pauseArr:Array;
            var info:* = param1;
            var _loc_4:int = 0;
            var _loc_5:* = info.ads;
            var _loc_3:* = new XMLList("");
            for each (_loc_6 in _loc_5)
            {
                
                var _loc_7:* = _loc_5[_loc_4];
                with (_loc_5[_loc_4])
                {
                    if (@type == AdType.PAUSE)
                    {
                        _loc_3[_loc_4] = _loc_6;
                    }
                }
            }
            listXML = _loc_3;
            if (listXML.length() > 0)
            {
                pauseArr;
                i;
                while (i < listXML.length())
                {
                    
                    itemXML = listXML[i];
                    itemInfo = new AdItemInfo(itemXML);
                    pauseArr.push(itemInfo);
                    i = (i + 1);
                }
                adData.setAdItemByType(pauseArr, AdType.PAUSE);
            }
            return;
        }// end function

        protected function getCornerAdInfo(param1:XML) : void
        {
            var listXML:XMLList;
            var itemXML:XML;
            var info:* = param1;
            var _loc_4:int = 0;
            var _loc_5:* = info.ads;
            var _loc_3:* = new XMLList("");
            for each (_loc_6 in _loc_5)
            {
                
                var _loc_7:* = _loc_5[_loc_4];
                with (_loc_5[_loc_4])
                {
                    if (@type == AdType.CORNER_AD)
                    {
                        _loc_3[_loc_4] = _loc_6;
                    }
                }
            }
            listXML = _loc_3;
            itemXML = listXML.length() > 0 ? (listXML[0]) : (null);
            if (itemXML != null)
            {
                adData.setAdItemByType(new AdItemInfo(itemXML), AdType.CORNER_AD);
            }
            return;
        }// end function

        public function getAdItemByType(param1:int) : IAdItemInfo
        {
            return adData.getAdItemByType(param1);
        }// end function

        public function initFormerAdShowed() : void
        {
            adData.initFormerAdShowed();
            return;
        }// end function

        public function set isFormerAdShowed(param1:Boolean) : void
        {
            adData.isFormerAdShowed = param1;
            return;
        }// end function

        protected function getFrontLastVideoAdInfo(param1:XML) : void
        {
            var listXML:XMLList;
            var videoXML:XML;
            var info:* = param1;
            var _loc_4:int = 0;
            var _loc_5:* = info.ads;
            var _loc_3:* = new XMLList("");
            for each (_loc_6 in _loc_5)
            {
                
                var _loc_7:* = _loc_5[_loc_4];
                with (_loc_5[_loc_4])
                {
                    if (@type == AdType.FRONT_LAST)
                    {
                        _loc_3[_loc_4] = _loc_6;
                    }
                }
            }
            listXML = _loc_3;
            videoXML = listXML.length() > 0 ? (listXML[0]) : (null);
            if (videoXML != null)
            {
                adData.setAdItemByType(new AdItemInfo(videoXML), AdType.FRONT_LAST);
            }
            return;
        }// end function

        public function get adCacheList() : XMLList
        {
            return _adCacheList;
        }// end function

        public function getFromerAds() : Array
        {
            return adData.getFromerAds();
        }// end function

        protected function getOverlayAdInfo(param1:XML) : void
        {
            var listXML:XMLList;
            var itemXML:XML;
            var info:* = param1;
            var _loc_4:int = 0;
            var _loc_5:* = info.ads;
            var _loc_3:* = new XMLList("");
            for each (_loc_6 in _loc_5)
            {
                
                var _loc_7:* = _loc_5[_loc_4];
                with (_loc_5[_loc_4])
                {
                    if (@type == AdType.OVER_LAY)
                    {
                        _loc_3[_loc_4] = _loc_6;
                    }
                }
            }
            listXML = _loc_3;
            itemXML = listXML.length() > 0 ? (listXML[0]) : (null);
            if (itemXML != null)
            {
                adData.setAdItemByType(new AdItemInfo(itemXML), AdType.OVER_LAY);
            }
            return;
        }// end function

        public function addFormerAdShowed(param1:int) : void
        {
            adData.addFormerAdShowed(param1);
            return;
        }// end function

        protected function closeLoader() : void
        {
            if (loader)
            {
                loader.close();
                loader.removeEventListener(Event.COMPLETE, completeHlr);
                loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHlr);
                loader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHlr);
                loader.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHlr);
                loader = null;
            }
            return;
        }// end function

        protected function completeHlr(event:Event) : void
        {
            var event:* = event;
            try
            {
                parseAdItemInfo(XML(loader.data));
                dispatchEvent(new AdLoaderEvent(AdLoaderEvent.COMPLETE));
            }
            catch (error:Error)
            {
                ErrorTracker.send(mainInfo.sourceId, ErrorTracker.ADCONTROL_PARSE_XML_ERROR, 0, url, loader.data.toString());
            }
            return;
        }// end function

        protected function getBackVideoAdInfo(param1:XML) : void
        {
            var listXML:XMLList;
            var videoXML:XML;
            var info:* = param1;
            var _loc_4:int = 0;
            var _loc_5:* = info.ads;
            var _loc_3:* = new XMLList("");
            for each (_loc_6 in _loc_5)
            {
                
                var _loc_7:* = _loc_5[_loc_4];
                with (_loc_5[_loc_4])
                {
                    if (@type == AdType.BACK)
                    {
                        _loc_3[_loc_4] = _loc_6;
                    }
                }
            }
            listXML = _loc_3;
            videoXML = listXML.length() > 0 ? (listXML[0]) : (null);
            if (videoXML != null && !mainInfo.isListPlayer)
            {
                adData.setAdItemByType(new AdItemInfo(videoXML), AdType.BACK);
            }
            return;
        }// end function

        protected function parseAdItemInfo(param1:XML) : void
        {
            var _loc_4:String = null;
            var _loc_2:* = param1.adPlay[0];
            var _loc_3:* = param1.rid[0];
            if (_loc_3 != null)
            {
                _rurlId = String(_loc_3.toString());
            }
            if (_loc_2 != null)
            {
                _loc_4 = String(_loc_2.toString());
                TudouAdPlay.setValue(_loc_4);
            }
            adData.adThrowInfo = new AdThrowInfo();
            adData.adThrowInfo.areaCode = param1.@a;
            adData.adThrowInfo.channel = param1.@c;
            adData.adThrowInfo.virtualChannel = param1.@v;
            getFrontLastVideoAdInfo(param1);
            getOverlayAdInfo(param1);
            getCornerAdInfo(param1);
            getPlacardAndVideoInfo(param1);
            getLoadingAdInfo(param1);
            getBackgroundAdInfo(param1);
            getPauseAdInfo(param1);
            getBackVideoAdInfo(param1);
            getOnline(param1);
            _adCacheList = param1.cache;
            parseAllTid(param1);
            return;
        }// end function

        public function get rurlId() : String
        {
            return _rurlId;
        }// end function

        public function get isFormerAdShowed() : Boolean
        {
            return adData.isFormerAdShowed;
        }// end function

        private function httpStatusHlr(event:HTTPStatusEvent) : void
        {
            var _loc_2:String = null;
            switch(event.status)
            {
                case 0:
                case 200:
                {
                    break;
                }
                default:
                {
                    _loc_2 = event.status.toString();
                    break;
                    break;
                }
            }
            if (_loc_2)
            {
                ErrorReportTracker.send(mainInfo.iid, ErrorReportTracker.ADCONTROL_IOERROR, _loc_2);
                ErrorTracker.send(mainInfo.sourceId, ErrorTracker.ADCONTROL_IOERROR, 0, url, _loc_2);
                dispatchEvent(new AdLoaderEvent(AdLoaderEvent.IO_ERROR, false, false, _loc_2));
            }
            return;
        }// end function

        private function securityErrorHlr(event:SecurityErrorEvent) : void
        {
            ErrorReportTracker.send(mainInfo.iid, ErrorReportTracker.ADCONTROL_SECURITY_ERROR, event.toString());
            ErrorTracker.send(mainInfo.sourceId, ErrorTracker.ADCONTROL_SECURITY_ERROR, 0, url);
            dispatchEvent(new AdLoaderEvent(AdLoaderEvent.SECURITY_ERROR, false, false, event.toString()));
            return;
        }// end function

    }
}
